import { defineStore } from 'pinia'
import {
  ContactQueryCriteria,
  ContactService,
  ContactVo,
  FriendService,
  PageContactVo,
  PageUserApplyVo,
  PageUserVo,
  RoomGroupVo,
  RoomService,
  UserApplyCriteria,
  UserQueryCriteria,
  UserService,
  UserVo,
} from '/@/api/openapi'
import { ElMessage } from 'element-plus'
import { UserApplyState } from '/@/utils/types'

// 定义状态的类型 一般情况下Pinia会自动推断出state的类型，你就无需定义接口。如果state的数据类型比较复杂，推荐你定义一个接口来描述state的类型
export interface IChatState {
  allUser: PageUserVo
  userFriend: Array<UserVo>
  chatGroup: Array<RoomGroupVo>
  userApply: PageUserApplyVo
  userApplyMessage: UserApplyState
  contact: PageContactVo
  contactList: Array<ContactVo>
  contactMap: Map<number, ContactVo>
  currentRoomId: number
}

export const useUserFriendStore = defineStore('userFriend', {
  state: (): IChatState => ({
    allUser: {},
    userApply: {},
    chatGroup: new Array<RoomGroupVo>(),
    userFriend: new Array<UserVo>(),
    userApplyMessage: {},
    contact: {},
    contactList: new Array<ContactVo>(),
    contactMap: new Map<number, ContactVo>(),
    currentRoomId: -1,
  }),

  getters: {
    getAllUser(): PageUserVo {
      return this.allUser
    },
    getUserApply(): PageUserApplyVo {
      return this.userApply
    },
    getChatGroup(): Array<RoomGroupVo> {
      return this.chatGroup
    },
    getUserFriend(): Array<UserVo> {
      return this.userFriend
    },
    getContact(): PageContactVo {
      return this.contact
    },
  },
  actions: {
    updateContactByNewMessage(roomId: number) {
      //
      console.log('开始更新会话信息了')
      const tmp = new Array<ContactVo>()
      this.contactList.forEach(item => {
        // 如果是 当前房间则进行 未读数量+1
        // console.log(item.roomId, roomId, item.roomId === roomId, room, this.currentRoomId)
        if (String(item.roomId) === String(roomId)) {
          item.unreadCount += 1
        }
        if (String(item.roomId) === String(this.currentRoomId)) {
          item.unreadCount = 0
        }
        tmp.push(item)
      })
      this.contactList = tmp
      console.log(this.contactList)
      // 更新了会话信息。
      this.contact.records?.forEach(item => {
        if (item.roomId !== undefined) {
          this.contactMap.set(item.roomId, item)
        }
      })
    },
    async updateContact(searchParams: ContactQueryCriteria): Promise<number> {
      return await ContactService.getContactPage(searchParams).then(
        res => {
          if (
            res.code !== 200 ||
            res.data === undefined ||
            res.data.records === undefined
          ) {
            ElMessage({
              type: 'error',
              message: res.message,
            })
            return -1
          }
          this.contact = res.data
          this.contactList = res.data.records
          // 更新了会话信息。
          this.contact.records?.forEach(item => {
            if (item.roomId !== undefined) {
              this.contactMap.set(item.roomId, item)
            }
          })
          return 1
        },
        error => {
          ElMessage({
            type: 'error',
            message: error,
          })
          return -1
        },
      )
    },
    updateUserApplyMessage(data: UserApplyState) {
      this.userApplyMessage = data
    },
    async updateAllUser(searchParams: UserQueryCriteria) {
      const res = await UserService.queryAll(searchParams).then(
        res => {
          if (
            res.code !== 200 ||
            res.data === undefined ||
            res.data.records === undefined
          ) {
            ElMessage({
              type: 'error',
              message: res.message,
            })
            return
          }
          this.allUser = res.data
        },
        error => {
          ElMessage({
            type: 'error',
            message: error,
          })
        },
      )
    },
    updateUserFriend() {
      FriendService.queryUserFriend().then(
        res => {
          if (res.code !== 200 || res.data === undefined) {
            ElMessage({
              type: 'error',
              message: res.message,
            })
            return
          }
          this.userFriend = res.data
        },
        error => {
          ElMessage({
            type: 'error',
            message: error,
          })
        },
      )
    },

    async updateChatGroup() {
      const res = await RoomService.queryGroupList().then(
        res => {
          if (res.code !== 200 || res.data === undefined) {
            ElMessage({
              type: 'error',
              message: res.message,
            })
            return
          }
          this.chatGroup = res.data
        },
        error => {
          ElMessage({
            type: 'error',
            message: error,
          })
        },
      )
    },
    async updateUserApply(searchParams: UserApplyCriteria) {
      const res = await FriendService.queryUserApplyInfo(searchParams).then(
        res => {
          if (res.code !== 200 || res.data === undefined) {
            ElMessage({
              type: 'error',
              message: res.message,
            })
            return
          }
          this.userApply = res.data
        },
        error => {
          ElMessage({
            type: 'error',
            message: error,
          })
        },
      )
    },
  },
})
